package com.yiyi.credit.manage.sso.api.controller;

import com.yiyi.credit.manage.sso.model.dto.response.Response;
import com.yiyi.credit.manage.sso.model.entity.SysResource;
import com.yiyi.credit.manage.sso.service.inner.UserPermissionService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RefreshScope
@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private UserPermissionService userPermissionService;


    @ApiOperation(value = "获取用户当前系统的所有权限", notes = "获取用户当前系统的所有权限")
    @ApiImplicitParams
        ({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "sysInfoId", value = "系统id", required = true, dataType = "Long", paramType = "query")
        })
    @PostMapping(value = "/getSysUserPermission")
    public Object getSysUserPermission(@RequestParam Long userId, @RequestParam Long sysInfoId) throws Exception {

        //查询用户当前系统的所有权限
        List<SysResource> userRoleResourceList = userPermissionService.getSysUserPermission(userId, sysInfoId);
        return new Response<>(userRoleResourceList);
    }


    @ApiOperation(value = "获取用户当前系统的指定父节点子节点权限", notes = "获取用户当前系统的指定父节点子节点权限")
    @ApiImplicitParams
        ({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "sysInfoId", value = "系统id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "pid", value = "父节点id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "whetherAllChild", value = "是否查询所有子节点;true:是,false:否",
                dataType = "boolean", paramType = "query")
        })
    @PostMapping(value = "/getSysChildNodePermission")
    public Object getSysChildNodePermission(@RequestParam Long userId, @RequestParam Long sysInfoId,
                                            @RequestParam Long pid, boolean whetherAllChild) throws Exception {

        //获取用户当前系统的指定父节点子节点权限
        SysResource sysResource = userPermissionService.getSysChildNodePermission(userId,
            sysInfoId, pid, whetherAllChild);
        return new Response<>(sysResource);
    }


    @ApiOperation(value = "获取系统的所有资源", notes = "获取系统的所有资源")
    @ApiImplicitParams
        ({
            @ApiImplicitParam(name = "sysInfoId", value = "系统id", required = true, dataType = "Long", paramType = "query")
        })
    @PostMapping(value = "/getSysResource")
    public Object getSysResource(@RequestParam Long sysInfoId) throws Exception {

        //获取系统的所有资源
        List<SysResource> sysResourceList = userPermissionService.gerSysResourceBySysInfoId(sysInfoId);
        return new Response<>(sysResourceList);
    }

    @ApiOperation(value = "根据路径获取用户对应系统的权限信息", notes = "根据路径获取用户对应系统的权限信息")
    @ApiImplicitParams
        ({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "sysInfoId", value = "系统id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "url", value = "资源路径", required = true, dataType = "string", paramType = "query")
        })
    @PostMapping(value = "/getSysUserPermissionByUrl")
    public Object getSysUserPermissionByUrl(@RequestParam Long userId, @RequestParam Long sysInfoId,
                                            @RequestParam String url) throws Exception {

        //根据路径获取用户对应系统的权限信息
        SysResource sysResource = userPermissionService.getSysUserPermissionByUrl(userId, sysInfoId, url);
        return new Response<>(sysResource);
    }
}
